//
//  FDAlertView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/29.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDAlertView.h"
@interface FDAlertView()
/**背景view*/
@property (nonatomic, strong) UIView *bgView;
/**文字提示lb*/
@property (nonatomic, strong) UILabel *titleLb;
/**确定按钮*/
@property (nonatomic, strong) UIButton *sureBtn;
@end

@implementation FDAlertView

#pragma mark - 一 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
        //设置背景色
        self.backgroundColor = rgba(33,32,32,0.6);
    }
    return self;
}

- (void) setupView  {
    [self addSubview:self.bgView];
    
    [self.bgView addSubview:self.titleLb];
    [self.bgView addSubview:self.sureBtn];
}


#pragma mark - 二 getter

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 110)];
        _bgView.backgroundColor = kMainLightGrayColor;
        
        //切圆角
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 10;
    }
    return _bgView;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.backgroundColor = [UIColor whiteColor];
        _titleLb.text = @"不在配送范围内";
        _titleLb.font = [UIFont systemFontOfSize:14*kScaleFit];
        _titleLb.numberOfLines = 0;
        _titleLb.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLb;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_sureBtn setTitle:@"确定" forState:(UIControlStateNormal)];
        [_sureBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        _sureBtn.backgroundColor = [UIColor whiteColor];
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_sureBtn addTarget:self action:@selector(clickSureAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _sureBtn;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(270, 110));
        make.centerY.equalTo(self.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.bgView);
        make.height.mas_equalTo(60);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb.mas_bottom).offset(1);
        make.left.right.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView.mas_bottom);
    }];
}

#pragma mark - 三 接口

- (void)fd_showAlertViewWithTitle:(NSString *)title {
    //获取window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [window addSubview:self];
    
    self.titleLb.text = title;
    
}


- (void)clickSureAction:(UIButton *)btn {
    
    FDLog(@"点击了确定按钮");
    
    [self removeFromSuperview];
}

@end
